package com.sxtyxt.util;

import android.text.TextUtils;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateUtil {

    public static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDD = "yyyy-MM-dd";

    /**
     * 将Date类型转换为pattern格式的String串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 将String串转Date
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parse(String date, String pattern) throws ParseException {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.parse(date);
    }

    /**
     * @param date   yyyyMMddTHHmmss格式的日期
     * @param format 想得到的格式 例：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCommonFormatDate(String date, String format) {

        if (date == null || "".equals(date)) {
            return "";
        }
        Date d;
        try {
            d = parse(date, YYYYMMDDHHMMSS);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        return format(d, format);
    }

    public static String getDateFromLong(String date){
        if(TextUtils.isEmpty(date)){
            return "";
        }
        long dateL = Long.valueOf(date);
        return format(new Date(dateL),YYYYMMDD);
    }

    public static String getDateFromLong2(String date){
        if(TextUtils.isEmpty(date)){
            return "";
        }
        long dateL = Long.valueOf(date);
        return format(new Date(dateL),YYYYMMDDHHMMSS);
    }

    /**
     * 获取当前
     **/
    public static int getNowYear() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR);
    }

    public static int getNowMonth() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MONTH) + 1;
    }

    public static int getNowDay() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.DAY_OF_MONTH);
    }

    public static int getNowHour() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.HOUR_OF_DAY);
    }

    public static int getNowMinute() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MINUTE);
    }

    public static int getNowSecond() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.SECOND);
    }

    /**
     * 返回指定友好格式日期
     *
     * @return
     */
    public static String getFriendlyDate(String dateString, String pattern) {

        String myDate = "";
        try {
            Date date = parse(dateString, pattern);
            DateFormat fhhmm = new SimpleDateFormat("HH:mm");
            String hhmm = fhhmm.format(date);

            DateFormat fMMdd = new SimpleDateFormat("MM-dd");
            String MMdd = fMMdd.format(date);

            DateFormat fMMddHHmm = new SimpleDateFormat("MM-dd HH:mm");
            String MMddHHmm = fMMddHHmm.format(date);

            DateFormat fYYYYMMdd = new SimpleDateFormat("yyyy-MM-dd");
            String YYYYMMdd = fYYYYMMdd.format(date);

            long cDate = System.currentTimeMillis() / 1000;
            long mDate = date.getTime() / 1000;
            long mc = cDate - mDate;
            if (mc < 0) {
                myDate = MMddHHmm;
            } else if (mc <= 60) {// 时间间隔
                myDate = "刚刚";
            } else if (mc <= 60 * 60) {
                myDate = (int) (mc / 60) + "分钟前";
            } else if (mc <= 60 * 60 * 24) {
                myDate = (int) (mc / (60 * 60)) + "小时前";
            } else if (mc <= 60 * 60 * 24 * 2) {
                myDate = "昨天 " + hhmm;
            } else if (mc <= 60 * 60 * 24 * 3) {
                myDate = "前天 " + hhmm;
            } else if (mc <= 60 * 60 * 24 * 365) {
                myDate = MMddHHmm;
            } else {
                myDate = YYYYMMdd;
            }

            return myDate;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 返回0为< 1为= 2为>
     *
     * @param time1
     * @param time2
     * @return
     */
    public static int compareDate(String time1, String time2) {
        if (time1 == null || time2 == null) {
            return -1;
        }
        Log.e("time1-----", time1);
        Log.e("time2-----", time2);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);

            if (date1.getTime() < date2.getTime()) {
                return 0;
            }
            if (date1.getTime() == date2.getTime()) {
                return 1;
            }
            if (date1.getTime() > date2.getTime()) {
                return 2;
            }


        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
        return -1;
    }

    public static String getCurrentDay() {
        return format(new Date(), YYYYMMDDHHMMSS);
    }

    public static String getCurrentDay(String format) {
        return format(new Date(), format);
    }

    /**
     * 将毫秒数转成字符串
     * @param date
     * @param formatString
     * @return
     */
    public static String longToString(long date,String formatString) {
        Date dat = new Date(date);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dat);
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(formatString);
        String sb = format.format(gc.getTime());
        return sb;
    }


}
